package com.bee.mydiary.record;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.bee.mydiary.R;

public class RecordActivity extends Activity{
	private String TAG = "RecordActivity";
	private Button startrecord;
	private Button stoprecord;
	private Button startplay;
	private Button stopplay;
	private Button showlist;
	
	private MediaPlayer mediaplayer = null;
	private MediaRecorder mediarecorder = null;
	
	private String filename = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.record_layout);
		
		startrecord = (Button) findViewById(R.id.startrecord);
		startrecord.setOnClickListener(new StartRecordListener());
		stoprecord = (Button) findViewById(R.id.stoprecord);
		stoprecord.setOnClickListener(new StopRecordListener());
		startplay = (Button) findViewById(R.id.startplay);
		startplay.setOnClickListener(new StartPlayListener());
		stopplay = (Button) findViewById(R.id.stopplay);
		stopplay.setOnClickListener(new StopPlayListener());
		showlist = (Button) findViewById(R.id.showrecordlist);
		showlist.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(RecordActivity.this,ShowSoundList.class);
				startActivity(intent);
			}
		});
		filename = Environment.getExternalStorageDirectory().toString();
		filename = filename + "/mydiary/record/audiosoundrecord.3gp";
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	
	class StartRecordListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			mediarecorder = new MediaRecorder();
			mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
			mediarecorder.setOutputFile(filename);
			mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
			
			try{
				mediarecorder.prepare();
			}catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}
			mediarecorder.start();
		}
		
	}
	
	class StopRecordListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (mediarecorder != null) {
				mediarecorder.stop();
				mediarecorder.release();
				mediarecorder = null;
			}
		}
		
	}
	
	class StartPlayListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			mediaplayer = new MediaPlayer();
			try{
				mediaplayer.setDataSource(filename);
				mediaplayer.prepare();
				mediaplayer.start();
			}catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}
		}
		
	}
	
	class StopPlayListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(mediaplayer != null){
				mediaplayer.stop();
				mediaplayer.release();
				mediaplayer = null;
			}
		}
		
	}
	

}
